﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR.InteractionSystem;

public class Ban3Throwable : Throwable
{
    //定义一个爆炸特效预物体.
    public GameObject explosionEffect;
    //被复制出来的特效.
    GameObject createdEffect;

    //当本物体被扔出去的一瞬间，会触发此函数.
    protected override void OnDetachedFromHand(Hand hand)
    {
        //先执行基类的同名函数.
        base.OnDetachedFromHand(hand);

        Debug.LogWarning("我被扔出去了！！！！");
        //倒计时3秒，然后爆炸.
        Invoke("Explode", 3f);
    }

    //爆炸函数.
    void Explode()
    {
        //复制爆炸特效预物体.
        createdEffect = Instantiate(explosionEffect, transform.position, Quaternion.identity) as GameObject;
        //倒计时3秒，然后删除特效.
        Invoke("DestroyEffect", 3f);

        //炸弹爆炸了，检测周围有无敌人.
        int layerMask = LayerMask.NameToLayer("Enemy");
        layerMask = 1 << layerMask;
        Collider[] colArray = Physics.OverlapSphere(transform.position, 3, layerMask);
        if(colArray != null && colArray.Length>0)
        {
            //循环遍历数组中的每一个碰撞器.
            for(int i=0;i< colArray.Length;i++)
            {
                Ban3Enemy enemy = colArray[i].GetComponent<Ban3Enemy>();
                if(enemy!=null)
                {
                    enemy.OnHurt();
                }
            }
        }
    }

    //删除特效.
    void DestroyEffect()
    {
        if(createdEffect!=null)
            Destroy(createdEffect);
    }
}
